package org.jmhome.blog.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.jmhome.blog.model.Board;
import org.jmhome.blog.model.Category;
import org.jmhome.blog.service.BoardService;
import org.jmhome.blog.service.CategoryService;
import org.jmhome.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CategoryController {

	@Autowired
	private BoardService boardService;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private UserService userService;

	@ModelAttribute("allCategories")
	public List<Category> getAllCategories() {
		List<Category> allCategories = categoryService.getAllCategories();
		return allCategories;
	}
	
	@RequestMapping(value = "/allCategories.do", method = RequestMethod.GET)
	public ModelAndView getCategories(ModelAndView mav) {
		List<Category> allCategories = categoryService.getAllCategories();
		mav.addObject("allCategories", allCategories);
		mav.setViewName("board-lists");
		return mav;
	}

	@RequestMapping(value = "/category.do", method = RequestMethod.GET)
	public ModelAndView getCategory(ModelAndView mav,
			@RequestParam(value = "page", defaultValue = "0") int page,
			@RequestParam(value = "listCount", defaultValue = "10") int listCount,
			@RequestParam("categoryCode") String categoryCode) {
		
		int startNum = page * listCount;
		if(categoryCode == null  ) {
			categoryCode = "universe";
		}
		Category category = categoryService.getCategory(categoryCode);
		mav.addObject("categoryCode", category.getCategoryCode());
		mav.addObject("categoryName", category.getCategoryName());

		List<Board> boards = category.getBoard();
		int boardAllCount = boards.size();
		
		int toIndex = startNum + listCount;
		if(boardAllCount < toIndex) {
			toIndex = boardAllCount;
		}
		List<Board> currentBoards = boards.subList(startNum, toIndex);
		int pageCount = boardAllCount / listCount;

		mav.addObject("pageCount", pageCount);
		mav.addObject("boards", currentBoards);
		mav.setViewName("board-lists");
		return mav;
	}


	/*@RequestMapping(value = "/boardLists.do", method = RequestMethod.GET)
	public ModelAndView getBoards(
			@RequestParam(value = "page", defaultValue = "0") int page,
			@RequestParam(value = "listCount", defaultValue = "10") int listCount,
			@RequestParam(value = "category", required = false) String category,
			HttpSession session, ModelAndView mav) {

		mav.setViewName("board-lists");
		int startNum = page * listCount;
		if (category == null) {
			// User user = (User) session.getAttribute("blogUser");
			// category = user();
			category = "universe";
		}

		List<Board> boards = boardService.getCategoryBoards(startNum,
				listCount, category);
		int boardAllCount = boardService.getCategoryBoardAllCount(category);
		int pageCount = boardAllCount / listCount;
		mav.addObject("boards", boards);
		mav.addObject("pageCount", pageCount);
		mav.addObject("category", category);

		return mav;
	}*/
	/*
	 * @RequestMapping(value = "/categoryForm.do", method = RequestMethod.GET)
	 * public ModelAndView boardWriteAndUpdateForm( ModelAndView mav,
	 * 
	 * @RequestParam(value = "boardSeq", required = false) Integer boardSeq,
	 * 
	 * @RequestParam(value = "email") String email) { Board board =
	 * boardService.getBoard(boardSeq); mav.addObject("board", board);
	 * mav.addObject("email", email); mav.setViewName("board-write-and-update");
	 * return mav; }
	 */

	/*
	 * @RequestMapping(value = "/writeAndUpdateSubmit.do", method =
	 * RequestMethod.POST) public ModelAndView
	 * boardWriteAndUpdateSubmit(ModelAndView mav,
	 * 
	 * @ModelAttribute("Board") Board board,
	 * 
	 * @RequestParam(value = "email") String email) throws
	 * BlogUserNotFoundException { board.setRegDate(new Date()); User user =
	 * userService.getUser(email); if (user == null) { throw new
	 * BlogUserNotFoundException(); } board.setUser(user); if
	 * (board.getBoardSeq() == null) { boardService.insertBoard(board); } else {
	 * boardService.updateBoard(board); }
	 * mav.setViewName("redirect:/boardLists.do?email=" + email); return mav; }
	 */
}
